package com.miyu.client1.service.magic.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.dao.book.bookChapterDao;
import com.miyu.client1.dao.book.bookMagicDao;
import com.miyu.client1.pojo.book.BookChapter;
import com.miyu.client1.pojo.book.BookListInfo;
import com.miyu.client1.utils.ehcache.EhcacheUtil;
import com.miyu.client1.utils.magic.GetBeanUtil;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.Map;

/**
 * 多线程调用网页解析 加速保存 缓存导致数据保存丢失 临时直接保存
 */
public class initSaveData implements Pipeline {


    @Override
    public void process(ResultItems resultItems, Task task) {

        //
        if(resultItems.get("type").equals("1")){
            JSONObject  bookInfo = resultItems.get("bookInfo");
            String bookId = resultItems.get("bookId");
            JSONObject binfo = (JSONObject) EhcacheUtil.getInstance().get("bookEhcache","bookInfo");
            if (binfo!=null)
                binfo.put(bookId,bookInfo);
            else{
                JSONObject p = new  JSONObject();
                p.put(bookId,bookInfo);
                binfo= p;
            }
            EhcacheUtil.getInstance().put("bookEhcache", "bookInfo", binfo);
        }else if(resultItems.get("type").equals("2")){
         //   JSONObject binfo = (JSONObject) EhcacheUtil.getInstance().get("bookEhcache","chapterInfo");
            JSONObject  chapterInfo = resultItems.get("chapterInfo");
            EhcacheUtil.getInstance().put("bookEhcache", chapterInfo.getString("chapterId"), chapterInfo);
            bookMagicDao dao = GetBeanUtil.getBeanByClass(bookMagicDao.class);
            StringBuffer sql = new StringBuffer();
            sql.append("update book_chapter set chapter_context='").append(chapterInfo.getString("chapterContext")).append("' where chapter_id='").append(chapterInfo.getString("chapterId")).append("'");
            dao.doExecute(sql.toString());

          /*  if (binfo!=null){
                binfo.put(chapterInfo.getString("chapterId"),chapterInfo.getString("chapterContext"));
            }else {
                JSONObject p = new  JSONObject();
                p.put(chapterInfo.getString("chapterId"),chapterInfo.getString("chapterContext"));
                binfo= p;
            }
            EhcacheUtil.getInstance().put("bookEhcache", "chapterInfo", binfo);
*/
        }
    //    JSONObject bookInfo = resultItems.get("bookInfo");
//        JSONObject chapterInfo = resultItems.get("chapterInfo");
/*        bookMagicDao dao = GetBeanUtil.getBeanByClass(bookMagicDao.class);
        BookListInfo bli = bookInfo.getJSONObject("bookInfo").toJavaObject(BookListInfo.class);
        dao.save(bli);
        bookChapterDao ChapterDao = GetBeanUtil.getBeanByClass(bookChapterDao.class);
        List<BookChapter> cli = bookInfo.getJSONArray("chapterInfo").toJavaList(BookChapter.class);
        ChapterDao.saveBatch(cli,500);*/
/*
        for (Map.Entry<String, Object> cj  : chapterInfo.entrySet()) {
            JSONArray rs = (JSONArray) cj.getValue();
            List<BookChapter> cli = rs.toJavaList(BookChapter.class);
            ChapterDao.saveBatch(cli,800);
        }
*/


/*
        JSONObject p = new  JSONObject();

       JSONObject binfo = (JSONObject) EhcacheUtil.getInstance().get("bookEhcache","bookInfo");
       if (binfo!=null)
             binfo.put(bookId,bookInfo);
       else{
           p.put(bookId,bookInfo);
           binfo= p;
       }
        EhcacheUtil.getInstance().put("bookEhcache", "bookInfo", binfo);
        JSONObject ps = new  JSONObject();
        JSONObject cinfo = (JSONObject) EhcacheUtil.getInstance().get("bookEhcache","chapterInfo");
        if (cinfo!=null)
            cinfo.put(bookId,chapterInfo);
        else{
            ps.put(bookId,chapterInfo);
            cinfo= ps;
        }
        EhcacheUtil.getInstance().put("bookEhcache", "chapterInfo", cinfo);*/
    }
}
